package com.example.mqttledmenu;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.ListFragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FragmentMine extends ListFragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private View rootView;
    private ListView lv;
    private SimpleAdapter adapter;

    public FragmentMine() {

    }

    public static FragmentMine newInstance(String param1, String param2) {
        FragmentMine fragment = new FragmentMine();
        Bundle args = new Bundle();//页面信息传输
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        int[] images = {R.drawable.user_info, R.drawable.safety, R.drawable.setting};
        String[] names = {"修改头像", "帐号安全", "应用设置"};
        adapter = new SimpleAdapter(getActivity(), getData(images, names),
                R.layout.setting_adapter_item, new String[]{"img", "name"},
                new int[]{R.id.image, R.id.name});
        setListAdapter(adapter);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_mine, container, false);
        }
        initView();
        return rootView;
    }

    private void initView() {
        //处理首页的搜索,对于Fragment只能静态添加点击函数
        lv = rootView.findViewById(android.R.id.list);
    }

    public void onListItemClick(ListView l, View v, int position, long id) {
        if (position == 0) {
            //修改头像
            Intent intent = new Intent();
            //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setClass(getContext(), EditUserInfo.class);
            startActivity(intent);
            //Toast.makeText(getActivity(), "你点击了个人信息~", Toast.LENGTH_LONG).show();
        } else if (position == 1) {
            Toast.makeText(getActivity(), "帐号安全功能开发中~", Toast.LENGTH_LONG).show();
        } else if (position == 2) {
            Toast.makeText(getActivity(), "应用设置功能开发中~", Toast.LENGTH_LONG).show();
        }
        super.onListItemClick(l, v, position, id);
    }

    //对数据进行加载
    private List<? extends Map<String, ?>> getData(int[] images, String[] names) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < images.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("img", images[i]);
            map.put("name", names[i]);
            list.add(map);
        }
        return list;
    }
}